<?php

/**
 * db actions.
 *
 * @package    tercermilenio
 * @subpackage db
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class dbActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        //$this->forward('default', 'module');
    }

    public function executeRealizarBackup() {

        $db_server = sfConfig::get('app_db_server');
        $db_username = sfConfig::get('app_db_username');
        $db_password = sfConfig::get('app_db_password');
        $db_name = sfConfig::get('app_db_name');
        $pathWeb = sfConfig::get('sf_web_dir');
        $db_save_dir = $pathWeb . sfConfig::get('app_db_dir');

        try {


            $db_resource = mysql_connect($db_server, $db_username, $db_password);
            if ($db_resource) {
                if (mysql_select_db($db_name, $db_resource)) {
                    $backupFile = $db_name . "_" . date("Y-m-d-H-i-s") . ".sql";
                    $command = "mysqldump --opt --user=".$db_username.
                    " --password=".$db_password." ".$db_name." > "
                            . $db_save_dir . $backupFile;
                    //$result = null;
                    //$command = "mysqldump --opt --user=root --password=root tercer_milenio_dev >
                    //    /home/maxi/workspace/tm/web/backups/tercer_milenio_dev_2011.sql";
                    //exec($command);
                    exec($command);
                }
            }           

        } catch (sfException $e) {
            sfContext::getInstance()->getUser()->setFlash('error', 'El backup no pudo realizarse debido a algunos errores.');
            echo $e->printStackTrace();
        }
            sfContext::getInstance()->getUser()->setFlash('notice', 'El backup fue realizado exitosamente.');
            $this->redirect('db/index');
    }

    public function executeRealizarRestore() {
        sfContext::getInstance()->getUser()->setFlash('notice', 'La restauración de la BD fue realizada exitosamente.');


            $db_resource = mysql_connect($db_server, $db_username, $db_password);
            if ($db_resource) {
                if (mysql_select_db($db_name, $db_resource)) {
                    $backupFile = $db_name . "_" . date("Y-m-d-H-i-s") . ".sql";
                    $command = "mysqldump --opt --user=".$db_username.
                    " --password=".$db_password." ".$db_name." < "
                            . $db_save_dir . $backupFile;
                    //$result = null;
                    //$command = "mysqldump --opt --user=root --password=root tercer_milenio_dev >
                    //    /home/maxi/workspace/tm/web/backups/tercer_milenio_dev_2011.sql";
                    //exec($command);
                    exec($command);
                }
            }
        $this->redirect('db/index');
    }

}
